<template>
	<view class='userLay pageBg'>
		<custom-nav-bar title="我的" v-if="myCashData.lv>=1"></custom-nav-bar>
		<view class="userinfo">
			<view class="avatar" v-if="myCashData.avatarUrl">
				<image :src="myCashData.avatarUrl"  mode="aspectFill"></image>
			</view>
			
			<view class="avatar" v-if="!myCashData.avatarUrl">
				<image src="https://xinxinkc.online/static/upload/image/20241128/3f17b3d5b79a2206056644f1178bf2bb.png"  mode="aspectFill"></image>
			</view>
			
			
			
			<!-- //登录后 -->
			<view class="info" v-if="!isLogin">
				<view class="phone">
					{{myCashData.username||"未登录"}}
				</view>
				<view class="lv">
					<view class="text">
						{{myCashData.lvName||'学生'}}
					</view>
					
					
				</view>
			</view>
		</view>
		
		
		
		<!-- 辅导员操作 -->
		<view class="list2" v-if="myCashData.lv >= 1">
			<view class="cash">
				<view class="text">
					工作台
				</view>
			</view>
			
			<view class="content">
				
					<navigator url="/pages/teacher/teacher">
						<view class="box">
							
							<view class="title">
								<image src="../../static/excel.png"   mode="aspectFill"></image>
								<view class="text">
									资料导入
								</view>
							</view>
						</view>
					</navigator>
					
					<navigator url="/pages/teacher/student">
						<view class="box">
							
							<view class="title">
								<image src="../../static/student.png"   mode="aspectFill"></image>
								<view class="text">
									学员管理
								</view>
							</view>
						</view>
					</navigator>
					
					<navigator url="/pages/teacher/warning">
						<view class="box">
							
							<view class="title">
								<image src="../../static/yj.png"   mode="aspectFill"></image>
								<view class="text">
									预警管理
								</view>
							</view>
						</view>
					</navigator>
					
					<navigator url="/pages/teacher/count?type=1">
						<view class="box">
							
							<view class="title">
								<image src="../../static/tj.png"   mode="aspectFill"></image>
								<view class="text">
									信息统计
								</view>
							</view>
						</view>
					</navigator>
					
					<navigator url="/pages/teacher/communicate">
						<view class="box">
							
							<view class="title">
								<image src="../../static/gt.png"   mode="aspectFill"></image>
								<view class="text">
									沟通回访
								</view>
							</view>
						</view>
					</navigator>
					
			</view>
		</view>
		<!-- 辅导员操作 -->
		
		<!-- 辅导员操作 -->
		<view class="list2" v-if="myCashData.lv >= 1">
			<view class="cash">
				<view class="text">
					申报
				</view>
			</view>
			
			<view class="content">
				
					<navigator url="/pages/teacher/authentication">
						<view class="box">
							
							<view class="title">
								<image src="../../static/zlws.png"   mode="aspectFill"></image>
								<view class="text">
									身份认证
								</view>
							</view>
						</view>
					</navigator>
					
					<navigator url="/pages/teacher/declare">
						<view class="box">
							
							<view class="title">
								<image src="../../static/cg.png"   mode="aspectFill"></image>
								<view class="text">
									成果申报
								</view>
							</view>
						</view>
					</navigator>
					
					<navigator url="/pages/teacher/declaration">
						<view class="box">
							
							<view class="title">
								<image src="../../static/bg-h.png"   mode="aspectFill"></image>
								<view class="text">
									申报管理
								</view>
							</view>
						</view>
					</navigator>
					
					<navigator url="/pages/teacher/count?type=2">
						<view class="box">
							
							<view class="title">
								<image src="../../static/tj.png"   mode="aspectFill"></image>
								<view class="text">
									申报统计
								</view>
							</view>
						</view>
					</navigator>
					
					
			</view>
		</view>
		<!-- 辅导员操作 -->
		
		<!-- 辅导员操作 -->
		<view class="list2" v-if="myCashData.lv >= 1">
			<view class="cash">
				<view class="text">
					系统管理
				</view>
			</view>
			
			<view class="content">
					<navigator url="/pages/system/system">
						<view class="box">
							
							<view class="title">
								<image src="../../static/zz.png"   mode="aspectFill"></image>
								<view class="text">
									组织管理
								</view>
							</view>
						</view>
					</navigator>
					
					<navigator url="/pages/system/staff">
						<view class="box">
							
							<view class="title">
								<image src="../../static/ry.png"   mode="aspectFill"></image>
								<view class="text">
									人员管理
								</view>
							</view>
						</view>
					</navigator>
					
					<navigator url="/pages/system/permission">
						<view class="box">
							
							<view class="title">
								<image src="../../static/qx.png"   mode="aspectFill"></image>
								<view class="text">
									权限管理
								</view>
							</view>
						</view>
					</navigator>
					
					
			</view>
		</view>
		<!-- 辅导员操作 -->
		
		<!-- 学员操作 -->
		 
			<view class="studentSet" v-if="myCashData.lv < 1">
			  <navigator url="/pages/user/studentInfo">
				  <uni-card title="华农经管学院" extra="" thumbnail="https://xinxinkc.online/static/upload/image/20241128/3f17b3d5b79a2206056644f1178bf2bb.png" class="xx-card">
				  	
				  		<view class="row">
				  			<view class="left">
				  				学号
				  			</view>
				  			<view class="right">
				  				123455655
				  			</view>
				  		</view>
				  		
				  		<view class="row">
				  			<view class="left">
				  				姓名
				  			</view>
				  			<view class="right">
				  				刘德华
				  			</view>
				  		</view>
				  
				  </uni-card>
				  
			  </navigator>
				
				
			</view>
		
		<!-- 学员操作 -->
		
		<view class="list2">
			<!-- <view class="cash">
				<view class="text">
					我的功能
				</view>
			</view> -->
			
			<view class="content">
				
					
						<view class="box" v-if="myCashData.lv >=1">
							
							<view class="title">
								<image src="../../static/kf.png"   mode="aspectFill"></image>
								<view class="text">
									联系客服
									<button open-type="contact"></button>
								</view>
							</view>
						</view>
					
					
					
						<view class="box">
							
							<view class="title">
								<image src="../../static/out.png"   mode="aspectFill" @click="out"></image>
								<view class="text">
									退出登录
								</view>
							</view>
						</view>
					
					
				
					
			</view>
			
			
		</view>
		
		<!-- 版本公司信息 -->
		<view class="company">
			<view class="shang">
				
			</view>
			<view class="xia">
				v1.0.0
			</view>
		</view>
			
			<!-- <button @click="callBoss">打给老板</button> -->
			<!-- 获取用户信息 -->
		
			<!-- <button @getphonenumber="getphonenumber" open-type="getPhoneNumber">手机号一键登录</button>
			<button @click="getuserinfo">微信号一键登录</button> -->
			
		<!-- 登录弹窗 -->
		<uni-popup ref='loginPopup' type='bottom':safe-area="false" >
			<view class="loginPopup">
				<view class="popheader">
					<view>
						<image src="../../static/fx.png" mode="aspectFill"></image>
					</view>
					<view class="title">华农经管学院欢迎你</view>
					
				</view>
				
				<scroll-view scroll-y>
						<view class="content">
									<button class="shang" @click="getuserinfo">微信一键登录</button>
									<button class="xia">暂时跳过</button>
									<view class="row">
										<view class="left">
											<label>
												<checkbox value="" checked activeBorderColor="#1ec0ff" iconColor="#1ec0ff"/><text></text>
											</label>
										</view>
										<view class="right">
											用户协议巴拉巴拉
										</view>
									</view>
									
							
							
							
							 <view class="safe-area-shop">
							 	
							 </view>
							<view class="safe-area-inset-bottom">
								
							</view>
							
						</view>
				</scroll-view>
			
			</view>
		</uni-popup>
		<!-- 登录弹窗结束 -->
		
		<!-- 辅导员登录弹框 -->
		<uni-popup ref='counselorLoginPopup' type='bottom':safe-area="false" >
			<view class="counselorLoginPopup">
				<view class="popheader">
					<view>
						<image src="../../static/fx.png" mode="aspectFill"></image>
					</view>
					<view class="title">华农经管学院欢迎你</view>
			
				</view>
				
				<scroll-view scroll-y>
						<view class="content">
									
								<view class="number">
											<view class="title">辅导员编号</view>
											<input class="xx-input" type="number" placeholder="请输入编号" />
										</view>
								<view class="pwd">
									<view class="title">密码</view>
									<input class="xx-input" password type="text" placeholder="请输入密码" />
								</view>
										
								<button class="shang" @click="changeUserinfo">确认切换</button>
										
							 <view class="safe-area-shop">
							 	
							 </view>
							<view class="safe-area-inset-bottom">
								
							</view>
							
						</view>
				</scroll-view>
			
			</view>
		</uni-popup>
		<!-- 辅导员登录弹框结束 -->
		
	</view>
</template>

<script setup>
import { ref } from 'vue';
import CryptoJS from 'crypto-js';
import { apiLogin,apiUserInfo,apiLoginByCode} from '../../api/apis';
import {onLoad,onShow} from "@dcloudio/uni-app";
 
	const userNickname = ref();
	const loginPopup =ref(null);//学生登录弹框
	const counselorLoginPopup =ref(null);//辅导员登录弹框
	//定义是否登录
	const isLogin = ref(true);
	//定义我的资产数据和图像昵称
	const myCashData = ref({});
	
	onLoad((e)=>{
		
	})
	
	onShow(async(e)=>{
		let userInfo =uni.getStorageSync('userInfo');
		console.log(userInfo);
		
		if(userInfo){
			
			//请求接口获取数据
			let data ={
				id:userInfo.id,
				token:userInfo.token
			}
			
			let res = await apiUserInfo(data);
			
			if(res.code ==1){
				//信息一致
				myCashData.value={
					db1_balance:res.data.db1_balance,
					db11_balance:res.data.db11_balance,
					username:res.data.username,
					lv:userInfo.lv,
					lvName:userInfo.lvName,
					avatarUrl:userInfo.avatarUrls
				}
				
				//证明已登录
				isLogin.value = false;
				
				if(userInfo.lv<1){
					uni.hideTabBar();
				}
			}else{
				//删除登录信息
				
				uni.removeStorageSync('userInfo');
				//需要重新登录
				isLogin.value = true;
				//去登录
				uni.navigateTo({
					url:"/pages/user/login"
				})
			}
			
			
			
		}else{
			//需要重新登录
			isLogin.value = true;
			//去登录
			uni.navigateTo({
				url:"/pages/user/login"
			})
		
		}
	})
	
	function callBoss(){
		uni.makePhoneCall({
			phoneNumber:"13642885049"
		})
	}
	
	function navTo(){
		
	}
	
	//获取微信用户信息
	const getphonenumber = (e)=>{
		console.log(e);
	
	}
	
	const getuserinfo = ()=>{
		console.log(1111);
		// #ifdef MP-WEIXIN
		uni.getUserProfile({
			  desc: '授权登录',
			      success: function (infoRes) {
			        console.log(infoRes);
					
					uni.login({
					  provider: 'weixin',
					  success: function (loginRes) {
					    // 发送 res.code 到后台换取 openId, sessionKey, unionId
					    console.log(loginRes.code); // 打印code
					    // 假设调用后端API获取sessionKey和openId
						let data = {
							code:loginRes.code,
							mobileEncryptedData:infoRes.encryptedData,
							mobileIv:infoRes.iv
						}
					    loginByCode(data);
					  }
					});
					
			      }
		})
		// #endif
		
		// #ifndef MP-WEIXIN
		let uinfo = {
			data:{
				openid:"13712750849",
				nickName:"农大之子",
				avatarUrl:"../../static/2.webp"
			}
		}
		loginGo(uinfo);
		// #endif
	
		
		
	}
	
	const loginByCode = async (data)=>{
		console.log(data);
		let res = await apiLoginByCode(data);
		console.log(res);
		loginGo(res);//将openid当作账号传给登录方法
	}
	
	//学生登录弹框
	const login = ()=>{
		
		loginPopup.value.open();
	}
	
	//辅导员登录弹框
	
	const counselorLogin = ()=>{
		
		counselorLoginPopup.value.open();
	}
	
	// 一键登录 模拟数据先
	const loginGo = async(uinfo)=>{
		
		let phone = uinfo.data.openid;
		let ppp =    CryptoJS.MD5(phone.slice(-6)).toString();
		//传给后台
		let data ={
			address:phone,
			ppp
		}
		
		let res = await apiLogin(data);
		console.log(res);
		if(res.code==1){
			isLogin.value = false;
			myCashData.value ={
				db1_balance:res.data.db1_balance,
				db11_balance:res.data.db11_balance,
				username:uinfo.data.nickName,
				lv:0,
				lvName:res.data.lv,
				avatarUrl:uinfo.data.avatarUrl
				
			}
			//写入缓存
			
			let userInfo = {
				id:res.data.id,
				token:res.data.token,
				username:uinfo.data.nickName,
				avatarUrl:uinfo.data.avatarUrl,
				lv:0,
				lvName:res.data.lv,
			}
			
			uni.setStorageSync('userInfo',userInfo);
			
			uni.showToast({
				title:"登录成功",
				icon:"none"
			})
			//关闭弹窗
			loginPopup.value.close();
		}else{
			uni.showToast({
				title:"登录失败",
				icon:"none"
			})
		}
		
	}
	
	const closeYh = ()=>{
		shopShow.value = true;
		isYh.value = false;
		yhPopup.value.close();
	}
	
	//切换角色
	const changeUserinfo = ()=>{
		// console.log(myCashData.value);
		let user =uni.getStorageSync('userInfo');
		
		let userInfo = {
			id:user.id,
			token:user.token,
			username:user.nickName,
			avatarUrl:user.avatarUrl,
			lv:1,
			lvName:"辅导员"
		}
		uni.setStorageSync('userInfo',userInfo);
		
		myCashData.value.lv = userInfo.lv;
		myCashData.value.lvName = userInfo.lvName;
		
		uni.showToast({
			title:"切换成功",
			icon:"none"
		})
		//关闭弹窗
		counselorLoginPopup.value.close();
	}
	
	//退出登录
	const  out = ()=>{
		
		//删除登录信息
		myCashData.value = {};
		uni.removeStorageSync('userInfo');
		//需要重新登录
		isLogin.value = true;
		uni.showToast({
			title:"已退出",
			icon:"none"
		})
		
		uni.navigateTo({
			url:"/pages/user/login"
		})
		
		//显示tabbar
		uni.showTabBar();
	}
	
</script>

<style lang='scss' scoped>
	  .userLay{
		  .safe-area-shop{
		  	/* #ifdef H5 */
		  		height: 15vh;
		  	/* #endif */
		  	/* #ifndef H5 */
		  		height: 10vh;
		  	/* #endif */
		  }
		  
		  .userinfo{
			  display: flex;
			  flex-direction: column;
			  align-items: center;
			  justify-content: center;
			  padding: 0 30rpx;
			  padding-top: 50rpx;
			  .avatar{
				  image{
					width: 150rpx;
					height: 150rpx; 
					border-radius: 75rpx; 
				  }
				
			  }
			  .info{
				  margin-top: 30rpx;
				 
				  .phone{
					
					  font-size:32rpx;
					  font-weight: 600;
					  padding-bottom: 20rpx;
					  text-align: center;
				  }
				  .lv{
					  
					  display: flex;
					  .text{
						  margin: 0 auto;
						  width:120rpx;
						  height: 40rpx;
						  border-radius: 15rpx;
						  background: $xx-color-body;
						  text-align: center;
						  line-height: 40rpx;
						  color:#fff;
					  }
					  image{
						  padding-left:10rpx;
						  width:50rpx;
						  height: 50rpx;
					  }
				  }
			  }
			  .ip{
				  
				  padding-top: 44rpx;
				  font-size: 28rpx;
				  color: #333;
				  
			  }
			  
			  .address{
				  margin-top:20rpx;
				  width: 180rpx;
				  height: 50rpx;
				  background: $xx-color-body;
				  border-radius: 25rpx;
				  font-size: 26rpx;
				  line-height: 50rpx;
				  text-align: center;
				  color: #fff;
			  }
		  }
		  
		  .list1{
			  background-color:#fff;
			  margin: 50rpx auto;
			  width:690rpx;
			  border: 1px solid #eee;
			  border-radius: 10rpx;
			  box-shadow:0rpx 10rpx 20rpx rgba(0, 0, 0, 0.05) ;
			  
			  
			  .cash{
				  height: 80rpx;
				  .text{
					  padding-left:20rpx;
					  line-height: 80rpx;
					  font-size: 30rpx;
					  font-weight: 550;
				  }
			  }
			  
			  .content{
				  padding:0 30rpx;
				  display: grid;
				  gap: 15rpx;
				  grid-template-columns: repeat(3,1fr);
				
				  .box{
					  padding-top: 30rpx;
					  text-align: center;
					  border-radius: 10rpx;
					  height: 150rpx;
					  .title{
						  display: flex;
						  justify-content: center;
						  align-items: center;
						  image{
							  width:50rpx;
							  height: 50rpx;
							  
						  }
						  .text{
							  font-weight: 600;
						  }
					  }
				  }
			  }
		  }
		  
		  .studentSet{
			  .row{
				  display: flex;
				  justify-content: space-between;
				  .left{
					  
				  }
				  
				  .right{
					 font-weight:600;
					 color: #000;
				  }
			  }
		  }
		  
		  .list2{
		  			  background-color:#fff;
		  			  margin: 50rpx auto;
		  			  width:690rpx;
		  			  border: 1px solid #eee;
		  			  border-radius: 10rpx;
		  			  box-shadow:0rpx 10rpx 20rpx rgba(0, 0, 0, 0.05) ;
		  			  
		  			  
		  			  .cash{
		  				  height: 80rpx;
		  				  .text{
		  					  padding-left:20rpx;
		  					  line-height: 80rpx;
		  					  font-size: 30rpx;
		  					  font-weight: 550;
		  				  }
		  			  }
		  			  
		  			  .content{
		  				  padding:0 30rpx;
		  				  display: grid;
		  				  gap: 15rpx;
		  				  grid-template-columns: repeat(4,1fr);
		  				
		  				  .box{
		  					  padding-top: 30rpx;
		  					  text-align: center;
		  					  border-radius: 10rpx;
		  					  height: 150rpx;
							  position: relative;
		  					  .title{
		  						
		  						  image{
		  							  width:50rpx;
		  							  height: 50rpx;
		  							  
		  						  }
		  						  .text{
									  font-size: 26rpx;
		  							  font-weight: 530;
		  						  }
		  					  }
							  
							  button{
								position: absolute;
								top:0;
								left:0;
								height: 100rpx;
								width:100%;
								opacity: 0;
							  }
		  				  }
		  			  }
		  }
		  
		  
		  .loginPopup{
		  	position: relative;
		  	background:#fff;
		  	padding: 30rpx;
		  	border-radius: 30rpx 30rpx 0 0;
		  	overflow: hidden;
		  	width:100vw;
		  	
		  	scroll-view{
		  		max-height: 60vh;
		  	}
		  	.popheader{
				image{
					width: 100rpx;
					height: 100rpx;
				}
				.title{
					font-size: 34rpx;
					font-weight: 550;
					padding-left: 10rpx;
				}
				.text{
					font-size: 28rpx;
					line-height: 50rpx;
					padding-left: 10rpx;
				}
			}
		  	.content{
					padding:50rpx 0;
		  			.shang{
						width: 500rpx;
						height: 80rpx;
						border-radius: 40rpx;
						background: $xx-color-body;
						color: #fff;
					}
					button::after{ border: none;}
					button{
						line-height:80rpx;
					}
					.xia{
						width: 500rpx;
						height: 80rpx;
						margin-top:20rpx;
						border-radius: 40rpx;
						color: $xx-color-body;
					}
					
					.row{
						display: flex;
						justify-content: space-around;
						align-items: center;
						padding-top: 30rpx;
						
						.left{
							
							
						}
						.right{
							flex: 1;
							width:0;
							word-wrap: break-word;
							padding-left:5rpx;
							font-size: 26rpx;
						}
					}
		  			
		  		
		  	}
		  }
		  
		  .counselorLoginPopup{
		  	position: relative;
		  	background:#fff;
		  	padding: 30rpx;
		  	border-radius: 30rpx 30rpx 0 0;
		  	overflow: hidden;
		  	width:100vw;
		  	
		  	scroll-view{
		  		max-height: 60vh;
		  	}
		  	.popheader{
		  				image{
		  					width: 100rpx;
		  					height: 100rpx;
		  				}
		  				.title{
		  					font-size: 34rpx;
		  					font-weight: 550;
		  					padding-left: 10rpx;
		  				}
		  				.text{
		  					font-size: 28rpx;
		  					line-height: 50rpx;
		  					padding-left: 10rpx;
		  				}
		  			}
		  	.content{
		  			padding:50rpx 0;
					
					.number{
						margin: 0 auto;
						width: 500rpx;
						padding: 30rpx 0;
						
						.title{
							color: $xx-color-body;
							font-weight: 600;
						}
						
						input{
							border-bottom: 1rpx solid $xx-color-body;
							height: 80rpx;
						}
						
					}
					.pwd{
						margin: 0 auto;
						width: 500rpx;
						padding: 30rpx 0;
						
						.title{
							color: $xx-color-body;
							font-weight: 600;
						}
						
						input{
							border-bottom: 1rpx solid $xx-color-body;
							height: 80rpx;
						}
					}
		  			.shang{
		  						width: 500rpx;
		  						height: 80rpx;
		  						border-radius: 40rpx;
		  						background: $xx-color-body;
		  						color: #fff;
		  					}
		  					button::after{ border: none;}
		  					button{
		  						line-height:80rpx;
		  					}
		  					.xia{
		  						width: 500rpx;
		  						height: 80rpx;
		  						margin-top:20rpx;
		  						border-radius: 40rpx;
		  						color: $xx-color-body;
		  					}
		  					
		  					.row{
		  						display: flex;
		  						justify-content: space-around;
		  						align-items: center;
		  						padding-top: 30rpx;
		  						
		  						.left{
		  							
		  							
		  						}
		  						.right{
		  							flex: 1;
		  							width:0;
		  							word-wrap: break-word;
		  							padding-left:5rpx;
		  							font-size: 26rpx;
		  						}
		  					}
		  			
		  		
		  	}
		  }
		  
		  .company{
			  padding: 100rpx 30rpx;
			  text-align: center;
			  font-size: 26rpx;
			  .shang{
				  
			  }
			  .xia{
				  
			  }
		  }
	  }     
</style>
